<?php

ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\tools\TbkUnion;
use PhpOffice\PhpSpreadsheet\IOFactory;

$file = '/Users/yiwan/Downloads/年货节设计师RID表格.xlsx';

$spreadsheet = IOFactory::load($file);
$sheetCount = $spreadsheet->getSheetCount();
$sheetNames = $spreadsheet->getSheetNames();
for ($index = 0; $index < $sheetCount; $index++) {
    $list = [];
    $spreadsheet->setActiveSheetIndex($index);
    $sheetData = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
    if ($sheetData && is_array($sheetData)) {
        $i = 0;
        foreach ($sheetData as $i => $item) {
            // 前三行不做处理，第一行为商品表格介绍，第二行为商品表格标题，第三行为示例，第四行才为真实数据
            if ($i <= 1) {
                continue;
            }
            $id = trim($item['A']);
            $nick = trim($item['B']);
            $relationId = trim($item['C']);
            $list[] = [
                "id" => trim($item['A']),
                "nick" => trim($item['B']),
                "relation_id" => trim($item['C'])
            ];
        }
    }
}

//获取推广链接
$activityMaterialId = 20150318020009017;
$unionId = 'tmjznhjngzc';
$data = [];
foreach ($list as $row) {
    $relationId = $row['relation_id'];
    $res = TbkUnion::TbkActivityInfoGet($activityMaterialId,
        TbkUnion::ADZONE_ID4, TbkUnion::PID4, $relationId, $unionId);
    if ($res && $res['click_url']) {
        $clickUrl = $res['click_url'];
        $res = TbkUnion::TbkSpreadGet($clickUrl);
        $url = $res[0]['content'] ?? '';
        $row['url'] = $url;
        $data[] = $row;
    }

}


$title = ['序列', '设计师昵称', '渠道ID', '推广链接'];

$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$titCol = 'A';
foreach ($title as $key => $value) {
    // 单元格内容写入
    $sheet->setCellValue($titCol . '1', $value);
    $titCol++;
}
$sheet->getColumnDimension('A')->setWidth(15);
$sheet->getColumnDimension('B')->setWidth(15);
$sheet->getColumnDimension('C')->setWidth(15);
$sheet->getColumnDimension('D')->setWidth(20);
$row = 2; // 从第二行开始
foreach ($data as $item) {
    $dataCol = 'A';
    foreach ($item as $value) {
        // 单元格内容写入
        $sheet->setCellValue($dataCol . $row, $value);
        $dataCol++;
    }
    $row++;
}
$fileName = '天猫家装节设计师带货推广链接' . '.xlsx';
$filePath = dirname(__FILE__) . DIRECTORY_SEPARATOR . $fileName;
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="' . $fileName . '"');
header('Cache-Control: max-age=0');
header('Cache-Control: max-age=1');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header('Pragma: public'); // HTTP/1.0
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save($filePath);
var_dump('导出成功');

die;



